package org.csu.jpetstorespringversion.controller;

import org.csu.jpetstorespringversion.mapper.AccountMapper;
import org.csu.jpetstorespringversion.mapper.ProfileMapper;
import org.csu.jpetstorespringversion.mapper.SignOnMapper;
import org.csu.jpetstorespringversion.model.Account;
import org.csu.jpetstorespringversion.model.Profile;
import org.csu.jpetstorespringversion.model.SignOn;
import org.csu.jpetstorespringversion.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class RegisterController {
    @Autowired
    public Account account;
    @Autowired
    public SignOn signOn;

    @Autowired
    public Profile  profile;

    @Autowired
    public ProfileMapper profileMapper;
    @Autowired
    public SignOnMapper signOnMapper;
    @Autowired
    public AccountMapper accountMapper;
    @Autowired
    public AccountService accountService;

    @GetMapping("/RegisterForm")
    public String RegisterForm (){
        return "registerForm";
    }
    @GetMapping("/MyAccount")
    public String   MyAccount(){
        return "Account";
    }
    @PostMapping("/Account")
    public String PostAccount(@RequestParam("userid") String username ,@RequestParam("password") String password ,@RequestParam("password1") String password1 ,@RequestParam("firstname") String firstname ,@RequestParam("lastname") String lastname ,@RequestParam("email") String email ,@RequestParam("phone") String phone ,@RequestParam("addr1") String addr1 ,@RequestParam("addr2") String addr2 ,@RequestParam("city") String city ,@RequestParam("state") String state ,@RequestParam("zip") String zip ,@RequestParam("country") String country ,@RequestParam("langpref") String langpref ,@RequestParam("favcategory") String favcategory ,@RequestParam("mylistopt") String mylistopt ,@RequestParam("banneropt") String banneropt , Model model){
        account.setUserid(username);
        account.setCity(city);
        account.setEmail(email);
        account.setFirstname(firstname);
        account.setLastname(lastname);
        account.setAddr1(addr1);
        account.setAddr2(addr2);
        account.setState(state);
        account.setPhone(phone);
        account.setZip(zip);
        account.setCountry(country);
        account.setStatus(" ");
        signOn.setUsername(username);
        signOn.setPassword(password);
        profile.setUserid(username);
        profile.setLangpref(langpref);
        profile.setFavcategory(favcategory);
        profile.setMylistopt(mylistopt);
        profile.setBanneropt(banneropt);
        int result = accountMapper.insert(account);
        int result1 = signOnMapper.insert(signOn);
        int result2 = profileMapper.insert(profile);
        if (result1 == 1 && result == 1 && result2 == 1){
            model.addAttribute("msg","注册成功，请开始登陆！！");
            return "loginForm";
        }
        model.addAttribute("msg","注册失败，请稍后重试！！");
        return "registerForm";
    }

    @PostMapping("/Account/Exchange")
    public String UpdateAccount(@RequestParam("userid") String username ,@RequestParam("password") String password ,@RequestParam("password1") String password1 ,@RequestParam("firstname") String firstname ,@RequestParam("lastname") String lastname ,@RequestParam("email") String email ,@RequestParam("phone") String phone ,@RequestParam("addr1") String addr1 ,@RequestParam("addr2") String addr2 ,@RequestParam("city") String city ,@RequestParam("state") String state ,@RequestParam("zip") String zip ,@RequestParam("country") String country ,@RequestParam("langpref") String langpref ,@RequestParam("favcategory") String favcategory ,@RequestParam("mylistopt") String mylistopt ,@RequestParam("banneropt") String banneropt , Model model){
        System.out.println(mylistopt);
        System.out.println(banneropt);
        account.setUserid(username);
        account.setCity(city);
        account.setEmail(email);
        account.setFirstname(firstname);
        account.setLastname(lastname);
        account.setAddr1(addr1);
        account.setAddr2(addr2);
        account.setState(state);
        account.setPhone(phone);
        account.setZip(zip);
        account.setCountry(country);
        account.setStatus(" ");
        signOn.setUsername(username);
        signOn.setPassword(password);
        profile.setUserid(username);
        profile.setLangpref(langpref);
        profile.setFavcategory(favcategory);
        profile.setMylistopt(mylistopt);
        profile.setBanneropt(banneropt);
        int b = signOnMapper.updateById(signOn);
        System.out.println(signOn);
        System.out.println(account);
        System.out.println(profile);
        int a = accountMapper.updateById(account);
        int c = profileMapper.updateById(profile);
        System.out.println(c);
        if (a == 1 && b == 1 && c == 1){
            model.addAttribute("msg","修改成功，请继续使用！！");
        }else{
            model.addAttribute("msg","修改失败，请稍后重试！！");
        }
        return "Account";
    }
}
